/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entidad.Medicion ;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author JEFER
 */
public class MedicionDAO {
    private static EntityManagerFactory
            emf = Persistence.createEntityManagerFactory("CentralMeteorologicaPU");

    public void crear(Medicion object) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }

public boolean eliminar(int id) {

        EntityManager em = emf.createEntityManager();
        //em.getTransaction().begin();
        boolean ret = false;
        try {
            Medicion object = leer(id);
            em.getTransaction().begin();
            object=em.merge(object);
            em.getTransaction().commit();
            em.getTransaction().begin();
            em.remove(object);
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return ret;
        }
    }

    public Medicion leer(int ids) {

        EntityManager em = emf.createEntityManager();
        Medicion medic = null;
        Query q = em.createQuery("SELECT u from Medicion u " +
               "WHERE u.id LIKE :id")
                .setParameter("id", ids);
        try {
            medic = (Medicion) q.getSingleResult();
        } catch (NonUniqueResultException e) {
            medic = (Medicion) q.getResultList().get(0);
        } catch (NoResultException e) {
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            em.close();
            return medic;
        }
    }

    public boolean actualizar(int id, Medicion nuevoObjeto) {

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        boolean ret = false;
        try {
            Medicion object = leer(id);
            object.setPluviosidad(nuevoObjeto.getPluviosidad());
            object.setPresion(nuevoObjeto.getPresion() );
            object.setTemperatura(nuevoObjeto.getTemperatura());
            object.setId(nuevoObjeto.getId());
            em.merge(object);
            em.getTransaction().commit();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
            return ret;
        }
    }


    
    
}
